﻿using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Linq;
using BusinessLibrary;
using MMSWebsite.Models;
using System;
using MMSWebsite.Models.Asset;

namespace MMSWebsite.DataAccessManagement
{
    public class MasterDataAccessProvider
    {
        MMSEntities _entities = null;
        MMS_MASTER_DATA mmsMD = null;

        #region Add new master data
        /// <summary>
        /// Add new master data
        /// <para>Return true: if add new master data successfully, 
        /// false: if adding new master data unsuccessfully</para>
        /// </summary>
        /// <param name="md">Master Data</param>
        /// <returns>true: if add new master data successfully, 
        /// false: if adding new master data unsuccessfully</returns>
        public bool AddMasterData(MasterData md)
        {
            try
            {
                //add master data information
                mmsMD = new MMS_MASTER_DATA()
                {
                    code = md.code,
                    type = md.type,
                    name = md.name,
                    value = md.value,
                    description = md.description,
                    freeze = "0"
                    // CREATE DATE WAS TURN TRIGGER ON IN DB
                    // create_date = dbDate
                    // To do: lack of user context
                    // create_id = md.cid
                };
                _entities = new MMSEntities();
                _entities.AddToMMS_MASTER_DATAs(mmsMD);
                _entities.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                // To do: handle exception, write log
                return false;
            }
        }
        #endregion Add new master data

        #region Update master data
        /// <summary>
        /// Update master data
        /// <para>Return true: if update master data successfully, 
        /// false: if updating master data unsuccessfully</para>
        /// </summary>
        /// <param name="customer">master data</param>
        /// <returns>Return true: if update master data successfully, 
        /// false: if updating master data unsuccessfully</returns>
        public bool UpdateMasterData(MasterData md)
        {
            try
            {
                _entities = new MMSEntities();
                var eMD = _entities.MMS_MASTER_DATAs.Single(e => e.id == md.id);
                //update base information
                eMD.code = md.code;
                eMD.type = md.type;
                eMD.name = md.name;
                eMD.value = md.value;
                eMD.description = md.description;
                eMD.freeze = "0";
                // UPDATE DATE WAS TURN TRIGGER ON IN DB
                // update_date = dbDate
                // To do: lack of user context
                // update_id = md.cid

                _entities.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                // To do: handle exception, write log
                return false;
            }
        }
        #endregion Update master data

        #region Delete master data
        /// <summary>
        /// Delete master data
        /// <para>Return true: if Delete master data successfully, 
        /// false: if delete master data unsuccessfully</para>
        /// </summary>
        /// <param name="id">master data id</param>
        /// <returns>Return true: if delete master data successfully, 
        /// false: if delete master data unsuccessfully</returns>
        public bool DeleteMasterData(int id)
        {
            try
            {
                _entities = new MMSEntities();
                var eMD = _entities.MMS_MASTER_DATAs.Single(e => e.id == id);
                //update base information
                eMD.freeze = "1";
                // UPDATE DATE WAS TURN TRIGGER ON IN DB
                // update_date = dbDate
                // To do: lack of user context
                // update_id = md.cid

                _entities.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                // To do: handle exception, write log
                return false;
            }
        }
        #endregion Delete master data

        #region Get master data By Id
        /// <summary>
        /// Get master data By Id
        /// <para>Return master data. Caution: Can be empty</para>
        /// </summary>
        /// <param name="id">Master Data Id</param>
        /// <returns>Master Data</returns>
        public MasterData GetMDById(int id)
        {
            MasterData md = new MasterData();
            try
            {
                _entities = new MMSEntities();
                var mmsMD = _entities.MMS_MASTER_DATAs.Single(e => e.id == id && e.freeze.Equals(Constants.UNFREEZE));
                if (mmsMD != null)
                {
                    md.id = mmsMD.id;
                    md.code = mmsMD.code;
                    md.type = mmsMD.type;
                    md.name = mmsMD.name;
                    md.value = mmsMD.value;
                    if (!String.IsNullOrEmpty(mmsMD.description)) 
                    {
                        md.description = mmsMD.description;
                    }
                    md.cid = (mmsMD.create_id == null ? 0 : (int)mmsMD.create_id);
                    md.uid = (mmsMD.update_id == null ? 0 : (int)mmsMD.update_id);
                }
                return md;
            }
            catch (Exception ex)
            {
                // To do: handle exception, write log
                return md;
            }
        }
        #endregion Get master data By Id

        #region Get master data By value
        /// <summary>
        /// Get master data By Code
        /// <para>Return list of master data. Caution: Can be empty</para>
        /// </summary>
        /// <param name="code">Master data code</param>
        /// <returns>List of master date. Caution: Can be empty</returns>
        public List<MasterData> GetMDByValue(string value)
        {
            MasterData md = null;
            List<MasterData> lsMd = new List<MasterData>();
            try
            {
                _entities = new MMSEntities();
                var mmsMD = from master in _entities.MMS_MASTER_DATAs
                            where master.value == value && master.freeze.Equals(Constants.UNFREEZE)
                            orderby master.id
                            select master;
                if (mmsMD != null)
                {
                    foreach (MMS_MASTER_DATA entity in mmsMD)
                    {
                        md = new MasterData();
                        md.id = entity.id;
                        md.code = entity.code;
                        md.type = entity.type;
                        md.name = entity.name;
                        md.value = entity.value;
                        if (!String.IsNullOrEmpty(entity.description))
                        {
                            md.description = entity.description;
                        }
                        md.cid = (entity.create_id == null ? 0 : (int)entity.create_id);
                        md.uid = (entity.update_id == null ? 0 : (int)entity.update_id);
                        lsMd.Add(md);
                    }
                }
                return lsMd;
            }
            catch (Exception ex)
            {
                // To do: handle exception, write log
                return lsMd;
            }
        }
        #endregion

        #region Get master data By Code - Type
        /// <summary>
        /// Get master data By Code
        /// <para>Return list of master data. Caution: Can be empty</para>
        /// </summary>
        /// <param name="code">Master data code</param>
        /// <returns>List of master date. Caution: Can be empty</returns>
        public List<MasterData> GetMDByCode(string code)
        {
            MasterData md = null;
            List<MasterData> lsMd = new List<MasterData>();
            try
            {
                _entities = new MMSEntities();
                var mmsMD = from master in _entities.MMS_MASTER_DATAs
                            where master.code == code && master.freeze.Equals(Constants.UNFREEZE)
                            orderby master.id
                            select master;
                if (mmsMD != null)
                {
                    foreach (MMS_MASTER_DATA entity in mmsMD)
                    {
                        md = new MasterData();
                        md.id = entity.id;
                        md.code = entity.code;
                        md.type = entity.type;
                        md.name = entity.name;
                        md.value = entity.value;
                        if (!String.IsNullOrEmpty(entity.description))
                        {
                            md.description = entity.description;
                        }
                        md.cid = (entity.create_id == null ? 0 : (int)entity.create_id);
                        md.uid = (entity.update_id == null ? 0 : (int)entity.update_id);
                        lsMd.Add(md);
                    }
                }
                return lsMd;
            }
            catch (Exception ex)
            {
                // To do: handle exception, write log
                return lsMd;
            }
        }

        /// <summary>
        /// Get master data By Code And Type
        /// <para>Return list of master data. Caution: Can be empty</para>
        /// </summary>
        /// <param name="code">Master data code</param>
        /// <param name="type">Master data types</param>
        /// <returns>List of master date. Caution: Can be empty</returns>
        public List<MasterData> GetMDByCodeAndType(string code, string type)
        {
            MasterData md = null;
            List<MasterData> lsMd = new List<MasterData>();
            try
            {
                _entities = new MMSEntities();
                var mmsMD = from master in _entities.MMS_MASTER_DATAs
                            where master.code == code && master.type == type 
                            && master.freeze.Equals("0")
                            orderby master.id
                            select master;
                if (mmsMD != null)
                {
                    foreach (MMS_MASTER_DATA entity in mmsMD)
                    {
                        md = new MasterData();
                        md.id = entity.id;
                        md.code = entity.code;
                        md.type = entity.type;
                        md.name = entity.name;
                        md.value = entity.value;
                        if (!String.IsNullOrEmpty(entity.description))
                        {
                            md.description = entity.description;
                        }
                        md.cid = (entity.create_id == null ? 0 : (int)entity.create_id);
                        md.uid = (entity.update_id == null ? 0 : (int)entity.update_id);
                        lsMd.Add(md);
                    }
                }
                return lsMd;
            }
            catch (Exception ex)
            {
                // To do: handle exception, write log
                return lsMd;
            }
        }
        #endregion Get master data By Code - Type

        #region Get List Of Master Data
        /// <summary>
        /// Get List Of Master Data
        /// <para>Return List Of Master Data. Caution: Can be empty</para>
        /// </summary>
        /// <returns>List Of Master Data</returns>
        public List<MasterData> GetMDList()
        {
            List<MasterData> lsMd = new List<MasterData>();
            MasterData md = null;
            try
            {
                _entities = new MMSEntities();
                var lsMDEntities = from master in _entities.MMS_MASTER_DATAs
                                   where master.freeze.Equals(Constants.UNFREEZE)
                                   orderby master.id
                                   select master;
                if (lsMDEntities != null)
                {
                    foreach (MMS_MASTER_DATA entity in lsMDEntities)
                    {
                        md = new MasterData();
                        md.id = entity.id;
                        md.code = entity.code;
                        md.type = entity.type;
                        md.name = entity.name;
                        md.value = entity.value;
                        if (!String.IsNullOrEmpty(entity.description))
                        {
                            md.description = entity.description;
                        }
                        md.cid = (entity.create_id == null ? 0 : (int)entity.create_id);
                        md.uid = (entity.update_id == null ? 0 : (int)entity.update_id);
                        lsMd.Add(md);
                    }
                }
                return lsMd;
            }
            catch (Exception ex)
            {
                // To do: handle exception, write log
                return lsMd;
            }
        }
        #endregion Get List Of Master Data

    }
}